package com.snail.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.snail.model.dto.MistakesDto;
import com.snail.model.pojo.BizQuestion;
import com.snail.model.pojo.BizUserQuestionMistakes;
import com.snail.response.BaseResponse;
import com.snail.response.ResultUtils;
import com.snail.service.BizUserQuestionMistakesService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ysc
 * @descrpiton 用户的错题集合
 * @date 2025/7/22 11:48
 **/
@RestController
@RequestMapping("/mistake")
@RequiredArgsConstructor
public class BizQuestionMistakeController {
    private final BizUserQuestionMistakesService mistakesService;

    // 查询所有
    @GetMapping("/list")
    public BaseResponse<?> list() {
        List<BizUserQuestionMistakes> list = mistakesService.list();
        return ResultUtils.success(list);
    }

    // 根据用户 ID 查询
    @GetMapping("/listById")
    public BaseResponse<?> listByUserId() {
        int userId= StpUtil.getLoginIdAsInt();
        List<BizUserQuestionMistakes> list = mistakesService.list(new QueryWrapper<BizUserQuestionMistakes>()
                .eq("user_id", userId));
        return ResultUtils.success(list);
    }

    // 添加记录
    @PostMapping("/add")
    public BaseResponse<?> add(@RequestBody BizUserQuestionMistakes mistake) {
        Integer userId= StpUtil.getLoginIdAsInt();
        mistake.setUserId(userId);
        boolean saved = mistakesService.save(mistake);
        return saved ? ResultUtils.success("添加成功") : ResultUtils.error("添加失败");
    }

    // 删除记录（按主键 ID）
    @DeleteMapping("/delete/{id}")
    public BaseResponse<?> delete(@PathVariable Integer id) {
        boolean removed = mistakesService.removeById(id);
        return removed ? ResultUtils.success("删除成功") : ResultUtils.error("删除失败");
    }

    // 批量删除
    @PostMapping("/batchDelete")
    public BaseResponse<?> batchDelete(@RequestBody List<Integer> ids) {
        boolean removed = mistakesService.removeByIds(ids);
        return removed ? ResultUtils.success("批量删除成功") : ResultUtils.error("批量删除失败");
    }
    //查询详情
    @GetMapping("/listDetail")
    public BaseResponse<?> listMistakeQuestionsByUserId() {
        int userId = StpUtil.getLoginIdAsInt();
        List<MistakesDto> questionList = mistakesService.getMistakeQuestionsByUserId(userId);

        return ResultUtils.success(questionList);
    }
    @GetMapping("/search")
    public BaseResponse<?> searchMistakeQuestionsByContent(@RequestParam String keyword) {
        int userId = StpUtil.getLoginIdAsInt();
        List<MistakesDto> result = mistakesService.searchMistakeQuestionsByContent(userId, keyword);
        return ResultUtils.success(result);
    }
}
